package com.yongcheon.starhealthcare.friend.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.model.GraphUser;
import com.facebook.widget.ProfilePictureView;
import com.yongcheon.starhealthcare.R;
import com.yongcheon.starhealthcare.util.AndroidUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yongcheon on 2015-02-10.
 */
public class FriendsListAdapter extends BaseAdapter {
    private List<GraphUser> graphUsers;
    private Context context;
    private LayoutInflater Inflater;
    private ArrayList<String> headerIndex;

    public FriendsListAdapter(Context context, List<GraphUser> graphUsers){
        this.context = context;
        this.Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.graphUsers = graphUsers;
        headerIndex = new ArrayList<String>();
    }

    @Override
    public int getCount() {
        return graphUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return graphUsers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GraphUser graphUser =  graphUsers.get(position);
        if (convertView == null) convertView = Inflater.inflate(R.layout.friend_friends_listitem, parent, false);
        if(!headerIsExist(graphUser.getName())) visibleHeader(convertView, position, graphUser.getName());
        ProfilePictureView profilePictureView = (ProfilePictureView) convertView.findViewById(R.id.face_profile_img);
        TextView textView = (TextView)convertView.findViewById(R.id.list_text);
        profilePictureView.setProfileId(graphUser.getId());
        textView.setText(graphUser.getName());
        return convertView;
    }

    private void visibleHeader(View convertView, int position,String userName){
        String firstChar = userName.substring(0,1);
        View separatorView = (View) convertView.findViewById(R.id.item_separator);
        TextView textView = (TextView)convertView.findViewById(R.id.list_hdr);
        RelativeLayout relativeLayout = (RelativeLayout)convertView.findViewById(R.id.friends_relative_layout);
        if(position != 0) separatorView.setVisibility(View.VISIBLE);
        textView.setVisibility(View.VISIBLE);
        textView.setText(firstChar);

        relativeLayout.getLayoutParams().height = (int) AndroidUtil.pxFromDp(convertView.getContext(),90);
        relativeLayout.getLayoutParams().width = RelativeLayout.LayoutParams.MATCH_PARENT;
        relativeLayout.setLayoutParams(relativeLayout.getLayoutParams());
    }

    private boolean headerIsExist(String name){
        boolean returnBool = false;
        String firstChar = name.substring(0,1);
        if(headerIndex.contains(firstChar)){
            returnBool = true;
        }else{
            headerIndex.add(firstChar);
        }
        return returnBool;
    }
}
